




std::list<PrivacyItem*> setPrivacyList(std::string & listName, std::list<PrivacyItem*> & listItem) {
	itemLists[listName] = listItem;
	return listItem;
}


std::list<PrivacyItem*> setActivePrivacyList() {
	activeName = defaultName;
	return listItem[activeName];
}


void deletePrivacyList(std::string & listName) {
    // Remove the list from the cache
    itemLists.remove(listName);

	// Check if deleted list was the default list
	if ( !defaultName.empty() && listName == defaultName) {
		defaultName = "";
	}
}


std::list<PrivacyItem*> getActivePrivacyList() {
	// Check if we have the default list
	if ( !activeName.empty()) {
		return itemLists[activeName];
	}
}


std::list<PrivacyItem*> getDefaultPrivacyList() {
	// Check if we have the default list
	if(!defaultName.empty())
		return itemLists[defaultName];
	}
}


std::list<PrivacyItem*> getPrivacyList(std::string & listName) {
	itemLists[listName];
}


PrivacyItem * getItem(std::string & listName, int order) {
	std::list<PrivacyItem*> l = itemLists[listName];
	std::list<PrivacyItem*>::iterator it = l.begin();
	PrivacyItem * itemFound = NULL;
	while (itemFound == NULL && it != l.end()) {
		if ((*it)->getOrder() == order) {
			itemFound = *it;
		}
		it ++;
	}
	return itemFound;
}


bool changeDefaultList(std::string & newDefault) {
	std::map<std::string, std::list<PrivacyItem*>>::it = itemLists.find(newDefault);
	if (it != itemLists.end()) {
		defaultName = newDefault;
		return true;
	} 
	return false; 
}


void deleteList(std::string & listName) {
    itemLists.erase(listName);
}


std::string getActiveName() {
	return activeName;
}


void setActiveName(std::string & activeName) {
	this->activeName = activeName;
}


std::string getDefaultName() {
	return defaultName;
}


void setDefaultName(std::string & defaultName) {
	this->defaultName = defaultName;
}


std::map<std::string, std::list<PrivacyItem*>> getItemLists() {
	return itemLists;
}


bool isDeclineActiveList() {
	return declineActiveList;
}


void setDeclineActiveList(bool declineActiveList) {
	this->declineActiveList = declineActiveList;
}


std::list<std::string> getPrivacyListNames() {
	std::list<std::string> l;
	for(std::map<std::string, std::list<PrivacyItem*>>::iterator it = itemLists.begin();
			it != itemLists.end(); it ++) {
		l.add(it->first);
	}
	return l;
}


std::string getChildElementXML() {
	std::string buf("<query xmlns=\"jabber:iq:privacy\">");

	// Add the active tag
	if (declineActiveList) {
		buf.append("<active/>");
	} else {
		if (!activeName.empty()) {
			buf.append("<active name=\"").append(activeName).append("\"/>");
		}
	}
	// Add the default tag
	if (declineDefaultList) {
		buf.append("<default/>");
	} else {
		if (!defaultName.empty()) {
			buf.append("<default name=\"").append(defaultName).append("\"/>");
		}
	}

	// Add the list with their privacy items
	for(std::map<std::string, std::list<PrivacyItem*>>::iterator it = itemLists.begin();
			it != itemLists.end(); it ++) {
		std::string listName = it->first;
		std::list<PrivacyItem*> items = it->second;
		if (items.empty()) {
			buf.append("<list name=\"").append(listName).append("\"/>");
		} else {
			buf.append("<list name=\"").append(listName).append("\">");

			for (std::list<PrivacyItem*>::iterator item = items.begin(); 
					item != items.end(); it ++) {
				// Append the item xml representation
				buf.append((*item)->toXML());
			}
			// Close the list tag
			buf.append("</list>");
		}
	}

	// Add packet extensions, if any are defined.
	buf.append(getExtensionsXML());
	buf.append("</query>");
	return buf;
}


